// pages/orgAuditor/orgAuditor.js
// pages/member/member.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    orgId: '',
    count: 0,
    number: '',
    parts: [],
    isSou: false,
    souname: '',
    pageNumber:1,
    pageSize:10,
    userId:[],
    type:null,
    totalPages:1,
    cashApplyNumber:null,
  },
  focusbtn() {
    this.setData({
      isSou: true
    })
  },
  souinput(e) {
    var souname = e.detail.value.replace(/(^\s*)(\s*$)/g, "")
    this.setData({
      souname: souname,
      pageNumber:1,
      list:[],
    })
    // console.log('搜索', this.data.souname)
      this.getlist()
  },
  getlist() {
    var that = this
    console.log('成员列表')
    app.req({
      url: "orgmini/org/newOrgUserPage",
      data: {
        orgId: that.data.orgId,
        pageNumber: that.data.pageNumber,
        pageSize: 10,
        name: that.data.souname
      },
      method: "post",
      success: function (res) {
        if (res.code == 200) {
          var arr = res.data.page.rows
          for (var i = 0; i < arr.length; i++) {
            arr[i].flag = false
          }
          if (that.data.userId.length>0){
            for (var i = 0; i < arr.length; i++) {
              for (var j = 0; j < that.data.userId.length; j++) {
                if (arr[i].userId == that.data.userId[j].userId) {
                  arr[i].flag = true
                }
              }
            }
          }
          that.setData({
            list: that.data.list.concat(arr),
            number: res.data.page.total,
            totalPages: res.data.page.totalPages
          })
        }
      }
    });
  },
  getlists() {
    var that = this
    app.req({
      url: "orgmini/orgAccount/getSetupOrgCashUser",
      data: {
        orgId: that.data.orgId,
        type: that.data.type
      },
      method: "post",
      success: function (res) {
        if (res.code == 200) {
          that.setData({
            userId: res.data,
            count:res.data.length
          })
        }
      }
    });
  },
  singlebtn(e) {
    var that = this
    var index = e.currentTarget.dataset.index
    var arr = that.data.list
    var count = that.data.count
    var cz = 0
    var num = 0
    if (arr[index].flag) {
      arr[index].flag = false
      count--
    } else {
      arr[index].flag = true
      count++
    }

    for (var i = arr.length - 1; i >= 0; i--) {
      if (arr[i].flag) {
        num++
      }
    }
    that.setData({
      list: arr,
      count: count,
    })
    console.log('单选列表', that.data.count)
  },
  //确定
  okbtn() {
    var that = this
    for (var i = 0; i < that.data.list.length; i++) {
      if (that.data.list[i].flag) {
        that.data.parts.push(that.data.list[i].userId)
      }
    }
    console.log('选中列表', that.data.parts)
    if (that.data.type == 1 && that.data.cashApplyNumber > 0 && that.data.parts.length==0){
      wx.showModal({
        content: '存在未审批的提现申请，无法设置为“无需审批”',
        showCancel:false,
        confirmColor:'#2168EB',
        success(res) {
        if(res.confirm) {
          console.log('用户点击确定')
        } 
        }
      })
    }else{
      app.req({
        url: "orgmini/orgAccount/setupOrgCashUser",
        data: {
          orgId: that.data.orgId,
          type: that.data.type,
          userId: that.data.parts
        },
        method: "post",
        success: function (res) {
          if (res.code == 200) {
            wx.showToast({
              title: '设置成功',
            })
            wx.navigateBack({
              delta: 2
            })
          }
        }
      });
    }
  },
  //删除元素
  remove: function (array, val) {
    for (var i = 0; i < array.length; i++) {
      if (array[i] == val) {
        array.splice(i, 1);
      }
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      orgId: options.id,
      type: options.type,
      cashApplyNumber: options.cashApplyNumber
    })
    if(options.type==1){
      wx.setNavigationBarTitle({
        title: '设置提现审核人'
      })
    } else if(options.type == 2){
      wx.setNavigationBarTitle({
        title: '设置可提现人'
      })
    }
    this.getlists()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    that.setData({
      list:[]
    })
      that.getlist()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this
    console.log('成员列表加载')
    if(that.data.totalPages>that.data.pageNumber){
      that.setData({
        pageNumber: that.data.pageNumber+1
      })
      that.getlist()
    }
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})